const Koa = require("koa");
const app = new Koa();
const json = require("koa-json"); //用于将http响应的数据转换为json格式
const bodyParser = require("koa-bodyparser"); //解析http请求的消息体
const Router = require("@koa/router"); //()等同于直接调用
const cors = require("@koa/cors"); //允许跨域
const mongoose = require("mongoose");
//配置别名
const { addAliases } = require("module-alias");
addAliases({
  "@": __dirname,
});
//数据库地址配置
const { BASE_URL } = require("@/config/Account");
// 统一返回前端的接口数据格式：中间件
const responseHandler = require("@/config/result");
//捕获错误中间件
const errorHandler = require("@/config/abnormal");

const router = new Router();
app.use(cors());
app.use(json());
app.use(bodyParser());
app.use(responseHandler);
app.use(errorHandler);

//连接数据库
mongoose
  .connect(BASE_URL)
  .then((res) => {
    console.log("数据库连接成功");
  })
  .catch((err) => {
    console.log("数据库连接失败");
  });

//接口

//登录注册用户账号
const login = require("@/routes/user/index");
//游记
const article = require("@/routes/article/index");
//接口后台
const admin = require("@/routes/pc-admin/index");
//首页
const home = require("@/routes/home/index");
//搜索游记
const searchTravel = require("@/routes/search-travel/index");
//当地玩法
const found = require("@/routes/found/index");
//结伴
const companion = require("@/routes/companion/index");
//数据分析
const dataAnalysis = require("@/routes/pc-data-analysis/index");
router.use("/apif", login);
router.use("/apif", article);
router.use("/apif", admin);
router.use("/apif", home);
router.use("/apif", searchTravel);
router.use("/apif", found);
router.use("/apif", companion);
router.use("/apif", dataAnalysis);
app.use(router.routes()).use(router.allowedMethods()); //allowedMethods:路由的请求情况
app.listen(8900, () => {
  console.log("端口启动成功");
});
